<?php
namespace app\common\model;

use think\facade\Db;
use think\Model;

class LpFundLog extends Model
{
    /*
     *  添加变动记录
     *  1--质押；2--解押；3--解押失败
     * */
    public function addFundLog($fund_data){
        $uid = $fund_data['uid'];
        $money = $fund_data['money'];
        Db::startTrans();
        try {
            $userModel = new User();
            $user = $userModel->find($uid);
            if(empty($user['id'])){
                Db::rollback();
                return returnData('用户不存在');
            }
            if($money < 0 && $user['lp_amount'] < abs($money)){
                Db::rollback();
                return returnData('账户余额不足');
            }
            $data = [
                'uid' => $uid,
                'type' => $fund_data['type'],
                'money' => $money,
                'detail' => $fund_data['detail'] ?? '',
                'note' => $fund_data['note'] ?? '',
                'c_time' => time()
            ];
            $userModel->where("id", $uid)->inc("lp_amount", $money)->update();
            $id = $this->insertGetId($data);
            if($id > 0){
                Db::commit();
                return returnData('success',1);
            }
            Db::rollback();
            return returnData('失败');
        }catch (\Exception $e){
            Db::rollback();
            return returnData('失败');
        }
    }

}